package videos;

import models.Lib2;
import models.Listvid;
import processs.Getyou2;
import processs.NaviDraw;
import processs.Navi_draw2;
import processs.Navi_draw3;
import processs.Navi_draw4;
import processs.Navi_draw5;
import processs.Navi_draw6;
import processs.Navi_draw7;
import processs.Navi_draw8;
import processs.Navi_draw9;
import processs.Seapr;
import utility.Check_Net;
import utility.DbConnect;
import utility.SDialog;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import example.demo_navi.R;


public class MainActivity extends Activity{
  
	private DrawerLayout dl;
	private ListView lv1;
	private ActionBarDrawerToggle abd;
	private CharSequence titi="";
	private DbConnect dbc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_4);

     dbc=new DbConnect(MainActivity.this);
     Getyou2 gy=new Getyou2(responseHandler);
     if(Check_Net.isOnLine(MainActivity.this)){
     new Thread(gy).start();
     }else{
    	 SDialog.Dialognet(MainActivity.this);
     }
        titi="Choa38";
        getActionBar().setTitle(titi);

        dl=(DrawerLayout) findViewById(R.id.drawer_layout);
        lv1=(ListView) findViewById(R.id.drawer_list);
        lv1.setOnItemClickListener(new DrawerItemClickListener());
        abd=new ActionBarDrawerToggle(this, dl, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close)
        {

			@Override
			public void onDrawerClosed(View drawerView) {
				// TODO Auto-generated method stub
				getActionBar().setTitle(titi);
				invalidateOptionsMenu();
			}

			@Override
			public void onDrawerOpened(View drawerView) {
				// TODO Auto-generated method stub
				getActionBar().setTitle("38quechoa");
				invalidateOptionsMenu();
			}
        	
        };
        
        dl.setDrawerListener(abd);
        ArrayAdapter<String> aad=new ArrayAdapter<String>(getBaseContext(), R.layout.drawer_list_item, getResources().getStringArray(R.array.Cate_video));
        lv1.setAdapter(aad);
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        if (savedInstanceState == null) {
            displayView(0);
        }  
    }
    
   //-------------------------------------------------------------
  //Thao tac DB
    Handler responseHandler = new Handler() {
		public void handleMessage(Message msg) {
			populateListWithVideos(msg);
		};
	};
	
	
	private void populateListWithVideos(Message msg) {
		
		Lib2 lib = (Lib2) msg.getData().get(Getyou2.Listvid);
		
		for(Listvid i:lib.getPlayli()){
			dbc.insert_playlist(new Listvid(i.getLid(),i.getLiti(),i.getPdate(), i.getLithum(), i.getCat_id()));
		Log.d("LayLan",i.getLiti()+ "- " +i.getCat_id());}
	}
    
    @Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

    //---------------------------------------------------------------
    //Drawerlayout
    
	private class DrawerItemClickListener implements ListView.OnItemClickListener {
    	   
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            displayView(position);
        }
    }  
    
	private void displayView(int position) {
		
		Fragment fragment = null;
		String[] str=getResources().getStringArray(R.array.Cate_video);
		switch (position) {
		case 0:
			fragment = new Navi_draw9();
			break;
		case 1:
			fragment = new Navi_draw2();
			break;
		case 2:
			fragment= new Navi_draw3();
			break;
		case 3:
			fragment=new Navi_draw4();
			break;
		case 4:
			fragment=new Navi_draw5();
			break;
		case 5:
			fragment=new Navi_draw6();
			break;
		case 6:
			fragment=new Navi_draw7();
			break;
		case 7:
			fragment=new Navi_draw8();
			break;
		case 8:
			fragment=new NaviDraw();
			break;

		default:
			break;
		}

		if (fragment != null) {
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame, fragment).commit();

			
			lv1.setItemChecked(position, true);
			lv1.setSelection(position);
			setTitle(str[position]);
			dl.closeDrawer(lv1);
		} else {
	
			Log.e("MainActivity", "Error in creating fragment");
		}
	}
	
	@Override
	public void setTitle(CharSequence title) {
		// TODO Auto-generated method stub
		titi=title;
		getActionBar().setTitle(titi);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		abd.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		abd.syncState();
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
	//	boolean bo=dl.isDrawerOpen(lv1);
	//	menu.findItem(R.id.itse).setVisible(bo);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (abd.onOptionsItemSelected(item)) {
			return true;
		}
		switch (item.getItemId()) {
		case R.id.itse:
			Seaproc();
			return true;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.newmenu, menu);
        return true;
    }
	
	private void Seaproc(){
		Fragment fr=new Seapr();
		if(fr!=null){
			FragmentManager fm=getFragmentManager();
			fm.beginTransaction().replace(R.id.content_frame, fr).commit();
		}else{
			Log.e("MainActivity_2", "Error in creating fragment");
		}
			
	}
/*	@Override
	public void setTitle(CharSequence title) {
		// TODO Auto-generated method stub
		mTitle=title;
		getActionBar().setTitle(mTitle);
	}  */

    
/*	public void onListvidClicked(Listvid lv) {
		// TODO Auto-generated method stub
		Intent in=new Intent(getApplicationContext(), Main2.class);
		in.setData(Uri.parse(lv.getLid()));
	//	in.putExtra("holle", lv.getLid());
		Log.d("tester", lv.getLid());
		startActivity(in);
	}  */


}